﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Linq;
using System.Xml.Linq;

public partial class eduexamquestions : System.Web.UI.Page
{
    DataClassesDataContext SMSDC = new DataClassesDataContext();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime m_BeginTime = Convert.ToDateTime(Session["BeginTime"]);
        if (!Page.IsPostBack)
        {
            var text = (from t in SMSDC.CourseInfos
                     where t.CourseID == Convert.ToInt32(Request.QueryString["CourseID"])
                     select t.CourseName).Single();
            this.Label2.Text = text.ToString();

            int m_CourseID = Convert.ToInt32(Request.QueryString["CourseID"]);
            int m_viewNum = 1;
           
            SqlConnection objConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["smsConnectionString"].ConnectionString);
            DataSet ds = new DataSet();
            SqlDataAdapter da1 = new SqlDataAdapter("select ExamPaperID from ExamPaperDetailInfo where IsLocal<>1 and ExamPaperDetailInfo.IsDeleted<>1 and CourseID = '" + m_CourseID + "' order by newid()", objConnection);
            da1.Fill(ds, "ExamPaperNum");
            DataTable dt1 = ds.Tables["ExamPaperNum"];

            SqlDataAdapter da2 = new SqlDataAdapter("select QuestionNum,QuestionDes,Answer1,Answer2,Answer3,Answer4,RightAnswer from QuestionInfo,ExamPaperInfo,ExamPaperDetailInfo where QuestionInfo.QuestionID = ExamPaperInfo.QuestionID and ExamPaperInfo.PaperDetailID = ExamPaperDetailInfo.PaperDetailID and IsLocal<>1 and ExamPaperDetailInfo.IsDeleted<>1 and ExamPaperDetailInfo.ExamPaperID =" + Convert.ToInt32(dt1.Rows[0].ItemArray[0]) + "and CourseID = " + m_CourseID + " order by QuestionNum asc", objConnection);
            da2.Fill(ds, "ExamPaperInfo");
            DataTable dt2 = ds.Tables["ExamPaperInfo"];
            DataColumn colWork = new DataColumn("webanswer", System.Type.GetType("System.String"));
            dt2.Columns.Add(colWork);
            //colWork = new DataColumn("webanswer", System.Type.GetType("System.Bool"));
            //dt2.Columns.Add(colWork);

            this.lblqamount.Text = "试卷一共" + dt2.Rows.Count + "题";
            Session["questioninfo"] = dt2;
            setpage(m_viewNum);
            Session["m_viewNum"] = m_viewNum;
        }
    }

    //上一题
    protected void Button1_Click(object sender, EventArgs e)
    {
        int m_viewNum = (int)Session["m_viewNum"];
        DataTable dt = (DataTable)Session["questioninfo"];
             
        //显示上一题 
        m_viewNum = m_viewNum - 1;
        //this.lbleqn.Text = "第" + m_viewNum + "题";
        setpage(m_viewNum); 
        Session["m_viewNum"] = m_viewNum;
        Session["questioninfo"] = dt;   
    }

    //下一题
    protected void btnexaque_Click(object sender, EventArgs e)
    {
        int m_viewNum = (int)Session["m_viewNum"];
        DataTable dt = (DataTable)Session["questioninfo"];
      
        //显示下一题 
        m_viewNum = m_viewNum + 1;
        //this.lbleqn.Text = "第" + m_viewNum + "题";
        setpage(m_viewNum);
        Session["m_viewNum"] = m_viewNum;
        Session["questioninfo"] = dt;     
    }

    //交卷
    protected void Button2_Click(object sender, EventArgs e)
    {
        int m_viewNum = (int)Session["m_viewNum"];
        DataTable dt = (DataTable)Session["questioninfo"];
        //统计答对了的题目数
        int n_RightNum = 0;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if(dt.Rows[i].ItemArray[6].ToString().Trim()==dt.Rows[i].ItemArray[7].ToString().Trim())
                n_RightNum += 1;  
        }

         var nPassQuestionNum = (from a in SMSDC.CourseInfos
                 where a.CourseID == Convert.ToInt32(Request.QueryString["CourseID"])
                 select a.PassQuestionNum).Single().Value;

        if (n_RightNum >= nPassQuestionNum)
        {
            //已通过，继续判断是否在表StaffsInCourses存在记录
            int nReturnValue = Convert.ToInt32(SMSDC.Edu_GetStaffsInCoursesByCourseIDandStaffName(Membership.GetUser().ToString(), Convert.ToInt32(Request.QueryString["CourseID"])).ReturnValue);
            if (nReturnValue > 0)
            {
                //存在，update
                SMSDC.Edu_UpdateStaffsInCourses(Membership.GetUser().ToString(), Convert.ToInt32(Request.QueryString["CourseID"]), DateTime.Now.Date, "已通过",2);
            }
            else
            {
                //不存在，insert
                SMSDC.Edu_InsertStaffsInCourses(Membership.GetUser().ToString(), Convert.ToInt32(Request.QueryString["CourseID"]), DateTime.Now.Date,1, "已通过", 2);
            }
        }
        else
        {
            //未通过，继续判断是否在表StaffsInCourses存在记录
            int nReturnValue = Convert.ToInt32(SMSDC.Edu_GetStaffsInCoursesByCourseIDandStaffName(Membership.GetUser().ToString(), Convert.ToInt32(Request.QueryString["CourseID"])).ReturnValue);
            if (nReturnValue > 0)
            {
                //存在，update
                SMSDC.Edu_UpdateStaffsInCourses(Membership.GetUser().ToString(), Convert.ToInt32(Request.QueryString["CourseID"]), DateTime.Now.Date, "未通过", 0);
            }
            else
            {
                //不存在，insert
                SMSDC.Edu_InsertStaffsInCourses(Membership.GetUser().ToString(), Convert.ToInt32(Request.QueryString["CourseID"]), DateTime.Now.Date, 1, "未通过", 0);
            }
        }


        Session.Remove("m_viewNum");
        Session.Remove("questioninfo");
        Response.Redirect("~/eduExamEnd.aspx?CourseID=" + Request.QueryString["CourseID"] + "");
    }

    public void setpage(int m_nQuestionNum)
    {
        
        this.RadioButtonList1.Items.Clear();
        this.LabQuestion.Text = "";

        DataTable dt = (DataTable)Session["questioninfo"];

        //button的enabled
        //下一题
        if (m_nQuestionNum >= dt.Rows.Count )
            this.btnexaque.Enabled = false;
        else
            this.btnexaque.Enabled = true;

        //上一题
        if (m_nQuestionNum <= Convert.ToInt32(dt.Rows[0].ItemArray[0]))
            this.Button1.Enabled = false;
        else
            this.Button1.Enabled = true;

        this.lbleqn.Text = "第" + m_nQuestionNum + "题";

        //页面出题
        DataRow[] rows = dt.Select("QuestionNum = '" + m_nQuestionNum + "'");
        foreach (DataRow row in rows)
        {
            this.LabQuestion.Text = row["QuestionDes"].ToString();
            if (row["Answer1"].ToString().Trim() != "")
                this.RadioButtonList1.Items.Add("A." + row["Answer1"]);
            if (row["Answer2"].ToString().Trim() != "")
                this.RadioButtonList1.Items.Add("B." + row["Answer2"]);
            if (row["Answer3"].ToString().Trim() != "")
                this.RadioButtonList1.Items.Add("C." + row["Answer3"]);
            if (row["Answer4"].ToString().Trim() != "")
                this.RadioButtonList1.Items.Add("D." + row["Answer4"]);
            //如果是已经做过的题目
            if (row["webanswer"].ToString().Trim() != "")
                this.RadioButtonList1.SelectedIndex = SetRadioButtonList(row["webanswer"].ToString().Trim());
        }
    }

    public string GetRadioButtonList(int nIndex)
    {
        string m_strText ="";
        switch (nIndex)
        {
            case 0:
                m_strText = "A";
                break;
            case 1:
                m_strText = "B";
                break;
            case 2:
                m_strText = "C";
                break;
            case 3:
                m_strText = "D";
                break;
        }
        return m_strText;
    }

    public int SetRadioButtonList(string strValue)
    {
        int m_nIndex=10;
        switch (strValue)
        {
            case "A":
                m_nIndex = 0;
                break;
            case "B":
                m_nIndex = 1;
                break;
            case "C":
                m_nIndex = 2;
                break;
            case "D":
                m_nIndex = 3;
                break;
        }
        return m_nIndex;
    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int m_viewNum = (int)Session["m_viewNum"];
        DataTable dt = (DataTable)Session["questioninfo"];
        //查找当前一题，记录下结果
        if (this.RadioButtonList1.SelectedValue != "")
        {
            DataRow[] rows = dt.Select("QuestionNum = '" + m_viewNum + "'");
            foreach (DataRow row in rows)
                row["webanswer"] = GetRadioButtonList(this.RadioButtonList1.SelectedIndex);
        }
    }
}
